package com.gk.study.handler;

import com.gk.study.common.APIResponse;
import com.gk.study.service.ErrorLogService;
import com.gk.study.common.ResponeCode;
import com.gk.study.entity.ErrorLog;
import com.gk.study.utils.HttpContextUtils;
import com.gk.study.utils.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import jakarta.servlet.http.HttpServletRequest; // 更新为 jakarta.servlet.http
import java.util.Arrays;
import java.util.Map;

@RestControllerAdvice
public class CustomExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);

    @Autowired
    private ErrorLogService service;

    @ExceptionHandler(Exception.class)
    public APIResponse handleException(Exception ex) {
        logger.error("error log======>" + ex.getMessage(), ex);

        saveLog(ex);
        return new APIResponse(ResponeCode.FAIL, ex.getMessage());
    }

    /**
     * 保存异常日志
     */
    private void saveLog(Exception ex) {
        ErrorLog log = new ErrorLog();
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();

        if (request != null) { // 添加空检查以避免潜在的 NullPointerException
            log.setIp(IpUtils.getIpAddr(request));
            log.setUrl(request.getRequestURI());
            log.setMethod(request.getMethod());

            // 请求参数
            Map<String, String> params = HttpContextUtils.getParameterMap(request);
            if (!params.isEmpty()) {
                // 如果有JsonUtils.toJsonString()方法，确保它也已经更新到与Jakarta EE兼容的版本
                // log.setRequestParams(JsonUtils.toJsonString(params));
            }

            // 异常信息
            log.setContent(Arrays.toString(ex.getStackTrace()));
            log.setLogTime(String.valueOf(System.currentTimeMillis()));
        }

        // 保存日志
        if (log.getUrl() != null && !log.getUrl().isEmpty()) { // 确保URL不为空再保存
            service.createErrorLog(log);
        }
    }
}